package com.lnsoft.cac.server.handle.codec.frameMessage;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

@Data
/**
 * 心跳数据报
 * */
public class Heart_Msg {
    private int Clocktime_Stamp;        //4byte     电压监测仪当前时间（世纪秒）

    public ByteBuf toByteBuf() throws Exception{
        ByteBuf bf = Unpooled.buffer(4);
        if (Clocktime_Stamp <= 0) {
            throw new Exception("当前时间世纪秒设置异常.");
        }
        bf.writeIntLE(Clocktime_Stamp);
        return bf;
    }



    //  解析 [心跳数据报] 帧报文内容区 到消息体中
    public static Heart_Msg decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        Heart_Msg msg = new Heart_Msg();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        if (4 != len) {
            throw new Exception("frameBody 数据异常");
        }
        msg.setClocktime_Stamp(bf.getIntLE(0) & 0xFFFFFFFF);
        return msg;
    }
}
